//
//  MKStatusModel.swift
//  Weibo
//
//  Created by mac on 15/12/18.
//  Copyright © 2015年 MK. All rights reserved.
//

import UIKit

class MKStatusModel: NSObject {
    
    //微博创建时间
    var created_at: String?
    //微博来源
    var source: String?
    //微博ID
    var id: Int64 = 0
    //微博信息内容
    var text: String?
    
    //配图的数组
    var pic_urls: [[String : String]]? {
        didSet {
            guard let urlsArr = pic_urls else {
                return
            }
            //初始化一个空数组
            pic_nsurls = [NSURL]()
            for item in urlsArr {
                var urlString = item["thumbnail_pic"]!
                
                //print(urlString)//http://ww2.sinaimg.cn/thumbnail/8a0a7b36gw1ez6e4e022cj218s0p6n5y.jpg
                //thumbnail是压缩图片 bmiddle是中图 square是小中图 large是大图(不推荐使用，耗内存)
                //换成高清图片
                //
                urlString = urlString.stringByReplacingOccurrencesOfString("thumbnail", withString: "bmiddle")
                
                let url = NSURL(string: urlString)!
                pic_nsurls?.append(url)
            }
        }
    }
    //将配图转换成NSURL
    var pic_nsurls: [NSURL]?
    
    //微博作者的用户信息字段
    var user: MKStatusUserModel?
    
    //FIXME: - 转发微博对象
    var retweeted_status: MKStatusModel?
    
    init(dict: [String : AnyObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        //这是用户微博的信息数组
        if key == "user" {
            if let dict = value as? [String : AnyObject] {
                user = MKStatusUserModel(dic: dict)
                //print(user)
            }
            return
        }
        
        //print(key)
        
        //FIXME: - 转发微博的赋值
        if key == "retweeted_status" {
            if let dict = value as? [String : AnyObject] {
                retweeted_status = MKStatusModel(dict: dict)
                //print(retweeted_status)
            }
            return
        }
        
        //调用父类 给本类其他属性进行KVC赋值
        super.setValue(value, forKey: key)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description: String {
        let keys = ["created_at", "id", "source", "text", "user"]
        return dictionaryWithValuesForKeys(keys).description
    }
}
